package com.dc.controller;

import com.dc.baseController.BaseController;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.Address;
import com.dc.model.User;
import com.dc.service.AddressService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@Api("地址模块")
@RequestMapping("/Address")
public class AddressController extends BaseController {
    @Autowired
    AddressService addressService;
    @RequestMapping(value = "/selectPageAddress",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户名",name = "user_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "当前页面",name = "curr_page",defaultValue = "1",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "一页的最大条数",name = "page_size",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @ResponseBody
    @ApiOperation(value = "分页查询地址信息",httpMethod = "POST",notes = "查询")
    public BaseModel selectPageAddress (String user_name, Integer curr_page, Integer page_size, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return addressService.selectPageAddress(user_name,curr_page,page_size,baseModel);
    }
    @RequestMapping(value = "/addAddress",method = RequestMethod.POST)
    @ApiOperation(value = "新增地址",httpMethod = "POST",notes = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "地址",name = "address_name",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "详细地址",name = "detailed_address",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "收货人",name = "user_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "收货人手机号码",name = "address_phone",required = false,dataType = "String",paramType = "query")
    })
    @ResponseBody
    public BaseModel addOrder(Address address, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        address.setUser_name(user.getName());
        address.setUser_id(user.getUser_id());
        address.setIs_default(1);
        address.setIs_deleted(0);
        address.setVersion(0);
        return addressService.addAddress(address,baseModel);
    }
    @RequestMapping(value = "/updateAddress",method = RequestMethod.POST)
    @ApiOperation(value = "修改地址",httpMethod = "POST",notes = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "地址ID",name = "address_id",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "地址",name = "address_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "详细地址",name = "detailed_address",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "收货人",name = "user_name",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(value = "收货人手机号码",name = "address_phone",required = false,dataType = "String",paramType = "query")
    })
    @ResponseBody
    public BaseModel updateAddress(Address address, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return addressService.updateAddress(address,baseModel);
    }
    @RequestMapping(value = "/deletedOrder/{ids}",method = RequestMethod.POST)
    @ApiOperation(value="删除地址",httpMethod = "POST",notes = "删除")
    @ResponseBody
    public BaseModel deletedAddress(@PathVariable("ids") String ids, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return addressService.deletedAddress(ids,baseModel);
    }
    @RequestMapping(value = "/selectAddressById/{id}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按用户id查询地址信息",httpMethod = "POST",notes = "查询")
    public BaseModel selectOrderByid(@PathVariable(value = "id") int id, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return addressService.selectAddressById(id, baseModel);
    }
    @RequestMapping(value = "/selectAddressByid/{id}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按地址id查询地址信息",httpMethod = "POST",notes = "查询")
    public BaseModel selectAddressByid(@PathVariable(value = "id") int id, BaseModel baseModel, HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.LOGIN_TIMEOUT);
        }
        return addressService.selectAddressByid(id, baseModel);
    }
}
